package com.fgsecure.apjhnou.info;

import java.io.File;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.R.string;
import android.util.Log;

public class Global {
	private static Vector<Object> vObj;
	private static Integer category;
	private static Integer subject;
	private static Integer lesson;
	private static Integer item;

	public static Vector<Object> getvObj() {
		return vObj;
	}

	public static void setvObj(Vector<Object> listObject) {
		vObj = listObject;
	}

	public static Integer getCategory() {
		return category;
	}

	public static void setCategory(Integer category) {
		Global.category = category;
	}

	public static Integer getSubject() {
		return subject;
	}

	public static void setSubject(Integer subject) {
		Global.subject = subject;
	}

	public static Integer getLesson() {
		return lesson;
	}

	public static void setLesson(Integer lesson) {
		Global.lesson = lesson;
	}

	public static Integer getItem() {
		return item;
	}

	public static void setItem(Integer item) {
		Global.item = item;
	}
	public static Vector<Object> readXML() {
		Vector<Object> menu0 = new Vector<Object>();
		try {
			File fXmlFile = new File("/sdcard/MyFile/myFile.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
			Log.v("--------------------------", "Root element :"
					+ doc.getDocumentElement().getNodeName());
			NodeList nList = doc.getElementsByTagName("category");
			for (int i = 0; i < nList.getLength(); i++) {
				Node nNode = nList.item(i);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					// Log.v("-------------Menu1-------------", "Name : "+
					// eElement.getAttribute("name"));
					menu0.add(eElement.getAttribute("name"));
					menu0.add(getElementInfo("subject", eElement));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return menu0;
	}

	public static Vector<Object> getElementInfo(String tag, Element element) {
		NodeList nList = element.getElementsByTagName(tag);
		Vector<Object> menu1 = new Vector<Object>();
		for (int i = 0; i < nList.getLength(); i++) {
			Vector<Object> menu2 = new Vector<Object>();
			Node nNode = nList.item(i);
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				Element eElement1 = (Element) nNode;
				menu1.add(eElement1.getAttribute("name"));
				// Log.v("-------------Menu2-------------","Name : " +
				// eElement1.getAttribute("name"));
				NodeList nList2 = eElement1.getElementsByTagName("lessons");
				for (int i2 = 0; i2 < nList2.getLength(); i2++) {
					Vector<Object> menu3 = new Vector<Object>();
					Node nNode2 = nList2.item(i2);
					if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
						Element eElement2 = (Element) nNode2;
						menu2.add(eElement2.getAttribute("name"));
						// Log.v("-------------Menu3-------------", "Name : "+
						// eElement2.getAttribute("name"));
						NodeList nList3 = eElement2
								.getElementsByTagName("item");
						for (int i3 = 0; i3 < nList3.getLength(); i3++) {
							Node nNode3 = nList3.item(i3);
							if (nNode3.getNodeType() == Node.ELEMENT_NODE) {
								Element eElement3 = (Element) nNode3;
								// Log.v("-------------Menu4-------------","Name : "+
								// eElement3.getAttribute("name")+
								// eElement3.getAttribute("location"));
								menu3.add(eElement3.getAttribute("name"));
								menu3.add(eElement3.getAttribute("location"));
							}
						}
						menu2.add(menu3);
					}
				}
				menu1.add(menu2);
			}
		}
		return menu1;
	}

}
